# -*- coding: iso-8859-1 -*-
#
#    This is part of Malvinas Argentinas Messenger
#
#    Malvinas Argentinas Messenger is free software; you can redistribute
#    it and/or modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2 of the
#    License, or (at your option) any later version.
#
#    Malvinas Argentinas Messenger is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Malvinas Argentinas Messenger; if not, write to the Free
#    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#    MA  02110-1301  USA

import msgWin, inputPad
import curses

class FrmWin(msgWin.MsgWin):
	def __init__(self, mam, title = '', msg = [], buttons = [], width = 50, frmLines = []):
		self.mam = mam
		self.mam.ui.options_win = 1
		self.title = title
		self.msg = msg
		self.buttons = buttons
		self.width = width
		self.frmLines = frmLines
		self.buttonIndex = self.defaultButIndex()
		self.frmLineIndex = self.defaultFrmLineIndex()
		self.height = self.calculateHeight()
		self.inputPads = []
		# Creo la MsgWin
		msgWin.MsgWin.__init__(self, self.mam, self.title, self.msg, self.buttons, self.width)
		# Muestro la ventana
		self.show()
		self.mam.ui.winOpt2 = self

	def maxLenfrmLinesTitle(self):
		max = 0
		for l in self.frmLines:
			if len(l.title) > max:
				max = len(l.title)
		return max

	def defaultFrmLineIndex(self):
		default = 0
		for l in self.frmLines:
			if l.default:
				return self.frmLines.index(l)
				break

	def calculateHeight(self):
		height = 6
		for line in self.msg:
			if line.nl: 
				height += 1
		if len(self.msg):
			height += 1
		for line in self.frmLines:
			height += 1
		return height

	def rebuildWin(self):
		self.show()

	def manageInput(self, carac):
		# F4
		if carac == int(0x10c):
			return -1, ''
		# ENTER
		elif carac == int(0xa):
			return self.buttonIndex, self.frmLines
		# TAB
		elif carac == int(0x9):
			self.buttonIndex += 1
			if self.buttonIndex == len(self.buttons):
				self.buttonIndex = 0
		# KEY_DOWN
		elif carac == curses.KEY_DOWN:
			self.frmLineIndex += 1
			if self.frmLineIndex == len(self.frmLines):
				self.frmLineIndex = 0
		# KEY_UP
		elif carac == curses.KEY_UP:
			self.frmLineIndex -= 1
			if self.frmLineIndex == -1:
				self.frmLineIndex = len(self.frmLines) - 1
		# KEY_LEFT
		elif carac == curses.KEY_LEFT:
			self.inputPads[self.frmLineIndex].cursBack()
		# KEY_RIGHT
		elif carac == curses.KEY_RIGHT:
			self.inputPads[self.frmLineIndex].cursForward()
		# KEY_HOME
		elif carac == curses.KEY_HOME:
			self.inputPads[self.frmLineIndex].cursBegin()
		# KEY_END
		elif carac == curses.KEY_END:
			self.inputPads[self.frmLineIndex].cursEnd()
		# KEY_BACKSPACE | KEY_DELETE
		elif carac in [curses.KEY_BACKSPACE, curses.KEY_DC]:
			if not self.frmLines[self.frmLineIndex].enabled:
				return -2, ''
			if carac == curses.KEY_DC and \
			self.inputPads[self.frmLineIndex].pos <> \
			len(self.inputPads[self.frmLineIndex].buff):
				self.inputPads[self.frmLineIndex].cursForward()
				self.inputPads[self.frmLineIndex].delChar()
			elif carac == curses.KEY_BACKSPACE:
				self.inputPads[self.frmLineIndex].delChar()
			# Update frmLine text
			self.frmLines[self.frmLineIndex].text = self.inputPads[self.frmLineIndex].buff
		# LETRAS
		elif carac < 256 and carac > 31:
			if not self.frmLines[self.frmLineIndex].enabled:
				return -2, ''
			self.inputPads[self.frmLineIndex].addstr(chr(carac))
			# Update frmLine text
			self.frmLines[self.frmLineIndex].text = self.inputPads[self.frmLineIndex].buff
		self.refresh()
		return -2, ''

	def show(self):
		curses.curs_set(1)
		self.w.bkgd(self.mam.ui.COLORS.OPTIONS_WIN1)
		# Creo el borde
		self.mam.ui.rectangle(self.w, 0, 0, self.height - 1, self.width - 1)
		# Muestro title
		self.move(1,3)
		self.addstr(self.title, self.mam.ui.COLORS.OPTIONS_WIN2)
		# Muestro mensaje/s
		i = 3
		lineChars = 0
		for line in self.msg:
			if lineChars:
				lineChars += 1
			self.move(i, max(lineChars + (self.width - lineChars + 1 - len(line.text))/2, 3))
			self.addstr(line.text[:self.width-6], line.color)
			if line.nl: 
				i += 1
				lineChars = 0
			else:
				lineChars += len(line.text)
		# Muestro nombre de frmLines
		if len(self.msg):
			i += 1
		for line in self.frmLines:
			self.move(i, 3)
			self.addstr(line.title, self.mam.ui.COLORS.OPTIONS_WIN4)
			for num in range(self.maxLenfrmLinesTitle()-len(line.title)):
				self.addstr(" ")
			self.addstr(" :", self.mam.ui.COLORS.OPTIONS_WIN4)
			i += 1
		# Creo pads de frmLines, y cargo su contenido
		i = 3
		for line in self.msg:
			if line.nl: 
				i += 1
		if len(self.msg):
			i += 1
		self.inputPads = []
		for line in self.frmLines:
			if self.frmLines.index(line) == self.frmLineIndex:
				color = self.mam.ui.COLORS.OPTIONS_WIN5
			else:
				color = self.mam.ui.COLORS.OPTIONS_WIN1
			self.move(i, 3)
			ip = inputPad.InputPad(1, self.width-self.maxLenfrmLinesTitle()-9, \
			(self.mam.ui.maxY-self.height)/2+i, \
			self.maxLenfrmLinesTitle()+6+(self.mam.ui.maxX-self.width)/2)
			ip.p.bkgd(color)
			ip.addstr(line.text)
			ip.cursBegin()
			self.inputPads.append(ip)
			i += 1
		self.refresh()

	def refresh(self):
		# Muestro botones
		colorDef = self.mam.ui.COLORS.OPTIONS_WIN5
		colorNotDef = self.mam.ui.COLORS.OPTIONS_WIN1
		numBut = 0
		for but in self.buttons:
			self.move(self.height - 2, (self.width-self.lenButtons())/2 + numBut*9)
			numBut += 1
			if self.buttons.index(but) == self.buttonIndex:
				self.addstr(" " + but.descrip + " ", colorDef)
			else:
				self.addstr(" " + but.descrip + " ", colorNotDef)

		# Refresh de ventana
		self.w.refresh()

		# Refresh de los input pads
		for ip in self.inputPads:
			if self.inputPads.index(ip) == self.frmLineIndex:
				color = self.mam.ui.COLORS.OPTIONS_WIN5
			else:
				color = self.mam.ui.COLORS.OPTIONS_WIN1
			ip.p.bkgd(color)
			ip.refresh_inOptWin()

		# Dejo el cursor en el default inputpad
		self.inputPads[self.frmLineIndex].refresh_inOptWin()
